package com.hq.ims.data.service;

import com.hq.ims.data.dao.PermissionMapAuthorityDepartmentMapper;
import com.hq.ims.data.entity.CompanyDepartment;
import com.hq.ims.data.entity.PermissionMapAuthorityDepartment;
import com.hq.ims.data.vo.CompanyDepartmentMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 * @Detail 权限组-部门映射表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BasePermissionMapAuthorityDepartmentService extends BaseService<PermissionMapAuthorityDepartmentMapper, PermissionMapAuthorityDepartment> {

    @Resource
    private BaseCompanyDepartmentService baseCompanyDepartmentService;

    @Resource
    private BasePermissionAuthorityService basePermissionAuthorityService;

    @Resource
    private BasePermissionMapAuthorityRoleService permissionMapAuthorityRoleService;


    /**
     * 添加部门权限数据
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean bindDepartmentAuth(
            CompanyDepartmentMapstruct.BindCompanyDepartmentAuth saveVo
    ) {
        SpringBootValidateUtils.validate(saveVo);
        //检查部门是否存在
        CompanyDepartment companyDepartment = baseCompanyDepartmentService.getById(saveVo.getCompanyDepartmentId());
        ObjectUtils.isNullThrow(companyDepartment, "部门不存在");

        //删除部门已有模板角色
        this.lambdaUpdate()
                .eq(PermissionMapAuthorityDepartment::getCompanyDepartmentId, saveVo.getCompanyDepartmentId())
                .remove();

        if (ListUtils.isNotExistsEle(saveVo.getPermissionAuthorityIdList())) {
            return true;
        }

        List<PermissionMapAuthorityDepartment> permissionMapGroupDepartments = new ArrayList<>();
        saveVo.getPermissionAuthorityIdList().forEach(authId -> {
            //检查权限组是否存在
            basePermissionAuthorityService.notExistsTh(authId);
            //绑定数据
            PermissionMapAuthorityDepartment permissionMapGroupDepartment = new PermissionMapAuthorityDepartment();
            permissionMapGroupDepartment.setCompanyDepartmentId(saveVo.getCompanyDepartmentId());
            permissionMapGroupDepartment.setPermissionAuthorityId(authId);
            permissionMapGroupDepartment.setCompanyId(companyDepartment.getCompanyId());

            permissionMapGroupDepartments.add(permissionMapGroupDepartment);
        });
        return this.saveBatch(permissionMapGroupDepartments);
    }

    /**
     * 根据部门ID列表获取角色
     *
     * @param deptIdList 部门id
     */
    public List<String> getAuthIdListByDeptIdList(@NonNull List<String> deptIdList) {
        if (ListUtils.isNotExistsEle(deptIdList)) {
            return ListUtils.newEmptyList();
        }
        List<PermissionMapAuthorityDepartment> permissionMapGroupDepartmentList = this.lambdaQuery()
                .select(PermissionMapAuthorityDepartment::getPermissionAuthorityId)
                .in(PermissionMapAuthorityDepartment::getCompanyDepartmentId, deptIdList)
                .orderByAsc(PermissionMapAuthorityDepartment::getCreateDateTime)
                .list();
        if (ListUtils.isNotExistsEle(permissionMapGroupDepartmentList)) {
            return ListUtils.newEmptyList();
        }
        return permissionMapGroupDepartmentList.stream().map(PermissionMapAuthorityDepartment::getPermissionAuthorityId).distinct().collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        this.lambdaUpdate().eq(PermissionMapAuthorityDepartment::getCompanyId, companyId).remove();
    }

}
